
#include"vmesh.h"

#include"glheader.h"

#include <stdlib.h>
#include <stdio.h>
#include<math.h>

VMesh::VMesh (const char* filename)
{
	FILE * fp = fopen(filename, "rt");
	if(fp == NULL)
	{
		printf("Arquivo %s invalido\n", filename);
		exit(1);
	}
	char line[121];
	int id = 0;
	int g, j;
	float d1[3];
	float d2[3];
	char f_limits = 0;
	if(fgets(line, 121, fp) != NULL){
    sscanf(line, "%d %d", &n_vertices, &n_triangles);
    float * vertices = (float *) malloc(sizeof(float) * n_vertices * 3);
    float * v_normals = (float *) malloc(sizeof(float) * n_vertices * 3);
    triangles = (unsigned int *) malloc(sizeof(unsigned int) * n_triangles * 3);
    t_normals = (float *) malloc(sizeof(float) * n_triangles * 3);
		while(id < n_vertices && fgets(line, 121, fp) != NULL){
			sscanf(line, "%d %f %f %f", &g, &vertices[id*3], &vertices[id*3+1], &vertices[id*3+2]);
			//printf("Vertice %d = %f %f %f\n", g , vertices[id*3], vertices[id*3+1], vertices[id*3+2]);
			if(f_limits == 0)
			{//inicializando os limites
				xmin = vertices[id*3]; xmax = vertices[id*3];
				ymin = vertices[id*3+1]; ymax = vertices[id*3+1];
				zmin = vertices[id*3+2]; zmax = vertices[id*3+2];
				f_limits = 1;
			}
			else{//atualizando os limites
				if(xmin > vertices[id*3])
					xmin = vertices[id*3];
				if(xmax < vertices[id*3])
					xmax = vertices[id*3];
				
				if(ymin > vertices[id*3+1])
					ymin = vertices[id*3+1];
				if(ymax < vertices[id*3+1])
					ymax = vertices[id*3+1];
				
				if(zmin > vertices[id*3+2])
					zmin = vertices[id*3+2];
				if(zmax < vertices[id*3+2])
					zmax = vertices[id*3+2];
			}
			//inicializando as normais
			v_normals[id*3]     = 0;
			v_normals[id*3+1]   = 0;
			v_normals[id*3+2]   = 0;
			id++;
		}
		id = 0;
		while(id < n_triangles && fgets(line, 121, fp) != NULL )
		{
			sscanf(line, "%d %d %d %d", &g, &triangles[id*3], &triangles[id*3+1], &triangles[id*3+2]);
			for(j=0; j<3; j++)
			{
				d1[j] = vertices[triangles[id*3]*3+j] - vertices[triangles[id*3+1]*3+j];
				d2[j] = vertices[triangles[id*3+1]*3+j] - vertices[triangles[id*3+2]*3+j];
			}
			//calculando a normal do triangulo
			vs_normcrossprod(d1, d2, &t_normals[id*3]);
			//atualizando noraml x dos vertices do triangulo
			v_normals[triangles[id*3]*3]     = v_normals[triangles[id*3]*3]     + t_normals[id*3];
			v_normals[triangles[id*3+1]*3]   = v_normals[triangles[id*3+1]*3]   + t_normals[id*3];
			v_normals[triangles[id*3+2]*3]   = v_normals[triangles[id*3+2]*3]   + t_normals[id*3];
			//atualizando noraml y dos vertices do triangulo
			v_normals[triangles[id*3]*3+1]   = v_normals[triangles[id*3]*3+1]   + t_normals[id*3+1];
			v_normals[triangles[id*3+1]*3+1] = v_normals[triangles[id*3+1]*3+1] + t_normals[id*3+1];
			v_normals[triangles[id*3+2]*3+1] = v_normals[triangles[id*3+2]*3+1] + t_normals[id*3+1];
			//atualizando noraml z dos vertices do triangulo
			v_normals[triangles[id*3]*3+2]   = v_normals[triangles[id*3]*3+2]   + t_normals[id*3+2];
			v_normals[triangles[id*3+1]*3+2] = v_normals[triangles[id*3+1]*3+2] + t_normals[id*3+2];
			v_normals[triangles[id*3+2]*3+2] = v_normals[triangles[id*3+2]*3+2] + t_normals[id*3+2];
            
            //calculando normais do reflection mode (mao esquerda)
            
			id++;
		}
		for(id = 0; id<n_vertices; id++)
		{
			vs_normalize(&v_normals[id*3]);
		}
    SetVertArray(vertices);
    SetNormalArray(v_normals);
	}
}

VMesh::~VMesh (void)
{
	//free (vertices);
	free (t_normals);
	//free (v_normals);
	//free (triangles);
  //if(tex_coord_array != NULL)
    //free(tex_coord_array);
}

void VMesh::SetCoordArray(int t_type, float * v){
	tex_type = t_type;
	tex_coord = (float *) malloc(sizeof(float) * n_vertices * tex_type);
	int i;
	for(i=0; i<n_vertices*tex_type; i++)
		tex_coord[i] = v[i];
}

void VMesh::ActiveTexCoord(bool active){
    tex = active;
}

